package com.sicurezza.nfcrypto;

import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.progettoSicurezza.secUtility.HashMaker;

public class MasterPasswordActivity extends Activity {
	
	private SharedPreferences sp = this.getSharedPreferences("KeyPreferences", MODE_PRIVATE);
	private SharedPreferences.Editor spe = sp.edit();
	private final static String TAG = "MasterPasswordActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_master_password);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		Log.v(TAG, "Tutto predisposto");
		Button saveButton = (Button) findViewById(R.id.button2);
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText passText = (EditText) findViewById(R.id.editText2);
				String passwd = passText.getEditableText().toString();
				Log.v(TAG, "Elaboro il testo preso");
				HashMaker hm = new HashMaker(passwd.getBytes(), "SHA-256",getApplicationContext());
				String hashpasswd = hm.hashMake();
				Log.v(TAG, "Scrivo su shared preferences");
				spe.putString("MasterPassword", hashpasswd);
				spe.commit();
				Log.v(TAG, "Termino...");
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.master_password, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_master_password,
					container, false);
			return rootView;
		}
	}

}
